//
//  oldTopChooseScrollView.m
//  laonianbangSupport
//
//  Created by 勇拓 李 on 18/3/6.
//  Copyright © 2018年 zaiyou. All rights reserved.
//

#import "oldTopChooseScrollView.h"
#import "oldTopChooseSingleView.h"

@interface oldTopChooseScrollView()
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *left_distance;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *view_width;
@property (weak, nonatomic) IBOutlet UIView *backview;
@property (nonatomic, copy) NSArray *list;
@property (nonatomic, copy) NSArray *dataList;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *line_width;
@property (nonatomic, assign)CGFloat defaultWidth;
@property (nonatomic, assign)NSInteger type;
@end

@implementation oldTopChooseScrollView

- (void)initWithList:(NSArray *)array width:(CGFloat)width
{
    _list = array;
    _defaultWidth = width;
    self.frame = CGRectMake(0, 0, SCREEN_WIDTH, 52);
    _view_width.constant = [array count] * _defaultWidth;
    _line_width.constant = width - 20.f;
    [self layoutIfNeeded];
    for (int i = 0; i < [array count]; i++) {
        [self initSingleView:i title:array[i]];
    }
}

- (void)initDataSource:(NSArray *)array
{
    _dataList = array;
}

- (void)changeType
{
    _type = 1;
    self.backview.backgroundColor = [UIColor whiteColor];
}

- (void)initSingleView:(int)number title:(NSString *)str
{
    oldTopChooseSingleView *view = [[[NSBundle mainBundle] loadNibNamed:@"oldTopChooseSingleView"
                                                                   owner:self
                                                                 options:nil] objectAtIndex:0];
    view.frame = CGRectMake(number * _defaultWidth, 0, _defaultWidth, 50);
    [self.backview addSubview:view];
    view.no = number;
    view.type = _type;
    if (_type && (number == 0)) {
        view.backgroundColor = [UIColor whiteColor];
    }
    view.label_title.text = str;
    if (number == 0) {
        view.label_title.textColor = OLD_COLOR;
    }
    
    UITapGestureRecognizer * gestureChoose = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(gesChoose:)];
    [view addGestureRecognizer:gestureChoose];
}

- (void)gesChoose:( UITapGestureRecognizer * ) gesture
{
    oldTopChooseSingleView *view = (oldTopChooseSingleView *)gesture.view;
    
    [UIView animateWithDuration:.5 animations:^{
        _left_distance.constant = 10 + _defaultWidth * view.no;
        [self layoutIfNeeded];//调用更改约束的view 的父视图的layoutIfNeeded 不要掉自己self.lineView
    } completion:^(BOOL finished) {
        for (UIView *subview in view.superview.subviews) {
            if ([subview isKindOfClass:[oldTopChooseSingleView class]]) {
                [(oldTopChooseSingleView *)subview changeTitleColor:view.no];
            }
        }
    }];
    
    [self.delegate topScrollViewChoosed:view.no];
}

@end
